package com.demo.house.service.impl;

import com.demo.house.dao.HouseSourseDao;
import com.demo.house.dao.HouseUrlDao;
import com.demo.house.dao.UserDao;
import com.demo.house.entity.HouseSourse;
import com.demo.house.entity.HouseUrl;
import com.demo.house.entity.User;
import com.demo.house.service.HouseSourseService;
import com.demo.house.service.HouseUrlService;
import com.demo.house.vo.RequestVo.*;

import com.demo.house.vo.response.ResponseSelectAllVo;
import com.demo.house.vo.response.ResponseSelectMySelf;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public  class HouseSourseServiceImpl implements HouseSourseService {
    @Autowired
    private HouseUrlDao houseUrlDao;

    @Autowired
    private HouseSourseDao houseSourseDao;
    private User user;

    private final String uploadPath="D:\\img\\";

    @Autowired
    private UserDao userDao;

    @Autowired
    private HouseUrlService houseUrlService;


//修改状态
    @Override
    public int update(HouseSourse houseSourse) {
        return houseSourseDao.updateByPrimaryKey(houseSourse);
    }
    @Override
    public int delete(Integer integer){
        return houseSourseDao.deleteByPrimaryKey(integer);
    }


/*详情查询*/
    @Override
    public List<HouseUrlSourseVo> selectCaa(Integer id) {
        HouseSourse houseSourse = houseSourseDao.selectByPrimaryKey(id);
        /*if (houseSourse.getState()!=1){
            return null;
        }*/
        HouseUrl houseUrl = houseUrlDao.selectByPrimaryKey(houseSourse.getId());
        List<HouseUrlSourseVo> list = new ArrayList<>();
        HouseUrlSourseVo houseUrlSourseVo = new HouseUrlSourseVo();
        houseUrlSourseVo.setId(houseSourse.getId());
        houseUrlSourseVo.setTitle(houseSourse.getTitle());
        houseUrlSourseVo.setDetail(houseSourse.getDetail());
        houseUrlSourseVo.setTelephone(houseSourse.getTelephone());
        houseUrlSourseVo.setArea(houseSourse.getArea());
        houseUrlSourseVo.setFloor(houseSourse.getFloor());
        houseUrlSourseVo.setState(houseSourse.getState());
        houseUrlSourseVo.setPrice(houseSourse.getPrice());
        houseUrlSourseVo.setAddress(houseSourse.getAddress());
        houseUrlSourseVo.setHouseType(houseSourse.getHouseType());

        List<HouseSourse> houseSourses=houseSourseDao.selectCaa(id);
        for (HouseSourse houseSourse1:houseSourses){
            User user = userDao.selectByPrimaryKey(houseSourse.getUserId());
            houseUrlSourseVo.setNikename(user.getNikename());
            houseUrlSourseVo.setInformation(houseSourse.getInformation());

            String information = houseUrlSourseVo.getInformation();
            String[] s = information.split(" ");
            houseUrlSourseVo.setS(s);
            List<HouseUrl> houseUrls = houseUrlService.selectUrl2(houseSourse.getId());
//            for (HouseUrl houseUrl1 : houseUrls) {
            if (houseUrls.get(0) != null){
                houseUrlSourseVo.setUrlId1(houseUrls.get(0).getId());
                houseUrlSourseVo.setUrl1(houseUrls.get(0).getUrl());
            }
            if (houseUrls.get(1) != null){
                houseUrlSourseVo.setUrlId2(houseUrls.get(1).getId());
                houseUrlSourseVo.setUrl2(houseUrls.get(1).getUrl());
            }
            if (houseUrls.get(2) != null){
                houseUrlSourseVo.setUrlId3(houseUrls.get(2).getId());
                houseUrlSourseVo.setUrl3(houseUrls.get(2).getUrl());
            }
//            }

        }
        list.add(houseUrlSourseVo);
        return list;
    }

    /*查询房源表数据，图片*/
    @Override
    public List<ResponseSelectAllVo> selectAll() {
     /*   List<HouseSourse> houseSourses = houseSourseDao.selectAll();
        System.out.println("suoyou de :  "  + houseSourses);

        for (HouseSourse houseSourse : houseSourses) {
            HouseUrl houseUrl1 = houseUrlService.selectUrl(houseSourse.getId());
            houseSourse.setMainImg(houseUrl1.getUrl());
        }

        return houseSourses;*/
        List<ResponseSelectAllVo> list = new ArrayList<>();
        List<HouseSourse> houseSourses = houseSourseDao.selectAll();
        System.out.println("suoyou de :  " + houseSourses);

        for (HouseSourse houseSours : houseSourses) {
            ResponseSelectAllVo reponseSelectAll = new ResponseSelectAllVo();
            BeanUtils.copyProperties(houseSours,reponseSelectAll);
            List<HouseUrl> houseUrls = houseUrlService.selectUrl2(houseSours.getId());
            HouseUrl houseUrl1 = houseUrlService.selectUrl(houseSours.getId());
            User user = userDao.selectByPrimaryKey(houseSours.getUserId());
            reponseSelectAll.setNikename(user.getNikename());
            houseSours.setMainImg(houseUrl1.getUrl());
            if (houseUrls.get(0) != null){
                reponseSelectAll.setId1(houseUrls.get(0).getId());
                reponseSelectAll.setUrl1(houseUrls.get(0).getUrl());
            }
            list.add(reponseSelectAll);
        }
        return list;
    }

    /**房东查询
     * @param id
     * @return
     */
    @Override
    public List<ResponseSelectMySelf> selectUserId(Integer id) {
        List<ResponseSelectMySelf> list = new ArrayList<>();

        List<HouseSourse> houseSourses = houseSourseDao.selectUserId(id);
        for (HouseSourse houseSours : houseSourses) {

            ResponseSelectMySelf responseSelectMySelf = new ResponseSelectMySelf();
            BeanUtils.copyProperties(houseSours,responseSelectMySelf);

            List<HouseUrl> houseUrls = houseUrlService.selectUrl2(houseSours.getId());
            if (houseUrls.get(0) != null){
                responseSelectMySelf.setId1(houseUrls.get(0).getId());
                responseSelectMySelf.setUrl1(houseUrls.get(0).getUrl());
            }
            if (houseUrls.get(1) != null){
                responseSelectMySelf.setId2(houseUrls.get(1).getId());
                responseSelectMySelf.setUrl2(houseUrls.get(1).getUrl());
            }
            if (houseUrls.get(2) != null){
                responseSelectMySelf.setId3(houseUrls.get(2).getId());
                responseSelectMySelf.setUrl3(houseUrls.get(2).getUrl());
            }
            list.add(responseSelectMySelf);
        }
        
        return list;
    }

/*图片显示*/
    @SneakyThrows
    @Override
    public BufferedImage getImg(Integer id) {
        HouseUrl byid = houseUrlDao.getByid(id);
        System.out.println(byid);

        String path = uploadPath+byid.getUrl();
        System.out.println(path);
        File file = new File(path);
        BufferedImage read = ImageIO.read(file);
        return read;
    }

    /*模糊搜索*/
    @Override
    public List<RequsetSourseVo> blurry(RequsetSourseVo requsetSourseVo) {
        List<RequsetSourseVo> list = new ArrayList<>();
        List<HouseSourse> houseSourses = houseSourseDao.blurry(requsetSourseVo);
        for (HouseSourse houseSours : houseSourses) {
            List<HouseUrl> houseUrls = houseUrlService.selectUrl2(houseSours.getId());
            HouseUrl houseUrl1 = houseUrlService.selectUrl(houseSours.getId());
            User user = userDao.selectByPrimaryKey(houseSours.getUserId());

            requsetSourseVo.setNikename(user.getNikename());
            requsetSourseVo.setTitle(houseSours.getTitle());
            requsetSourseVo.setAddress(houseSours.getAddress());
            requsetSourseVo.setArea(houseSours.getArea());
            requsetSourseVo.setFloor(houseSours.getFloor());
            requsetSourseVo.setHouseType(houseSours.getHouseType());
            requsetSourseVo.setPrice(houseSours.getPrice());
            requsetSourseVo.setState(houseSours.getState());
            requsetSourseVo.setTelephone(houseSours.getTelephone());
            requsetSourseVo.setDetail(houseSours.getDetail());
            requsetSourseVo.setMainImg(houseUrl1.getUrl());
            if (houseUrls.get(0) != null){
                requsetSourseVo.setId1(houseUrls.get(0).getId());
                requsetSourseVo.setUrl1(houseUrls.get(0).getUrl());
            }
            list.add(requsetSourseVo);
        }
        return list;
        }


    //随机查询多个房源
    @Override
    public List<RandomqueryVo> Randomquery(RandomqueryVo randomqueryVo) {
        List<RandomqueryVo> list = new ArrayList<>();
        List<HouseSourse> houseSourses = houseSourseDao.Randomquery(randomqueryVo);
        for (HouseSourse houseSours : houseSourses) {
            RandomqueryVo randomqueryVo1 = new RandomqueryVo();
            BeanUtils.copyProperties(houseSours,randomqueryVo1);
            List<HouseUrl> houseUrls = houseUrlService.selectUrl2(houseSours.getId());
            HouseUrl houseUrl1 = houseUrlService.selectUrl(houseSours.getId());
            User user = userDao.selectByPrimaryKey(houseSours.getUserId());
            randomqueryVo1.setNikename(user.getNikename());
            randomqueryVo1.setTitle(houseSours.getTitle());
            randomqueryVo1.setAddress(houseSours.getAddress());
            randomqueryVo1.setArea(houseSours.getArea());
            randomqueryVo1.setFloor(houseSours.getFloor());
            randomqueryVo1.setHouseType(houseSours.getHouseType());
            randomqueryVo1.setPrice(houseSours.getPrice());
            randomqueryVo1.setState(houseSours.getState());
            randomqueryVo1.setTelephone(houseSours.getTelephone());
            randomqueryVo1.setDetail(houseSours.getDetail());
            randomqueryVo1.setMainImg(houseUrl1.getUrl());
            if (houseUrls.get(0) != null){
                randomqueryVo1.setId1(houseUrls.get(0).getId());
                randomqueryVo1.setUrl1(houseUrls.get(0).getUrl());
            }
            list.add(randomqueryVo1);
        }
        return list;
    }

    //浏览记录
    @Override
    public List<RequesBrowsingRecordsVo> getByids(int[] ids) {
        List<RequesBrowsingRecordsVo> list = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            RequesBrowsingRecordsVo requesBrowsingRecordsVo = new RequesBrowsingRecordsVo();
            HouseSourse houseSourse = houseSourseDao.selectByPrimaryKey(ids[i]);
            BeanUtils.copyProperties(houseSourse,requesBrowsingRecordsVo);
            List<HouseUrl> houseUrls = houseUrlService.selectUrl2(houseSourse.getId());
            HouseUrl houseUrl1 = houseUrlService.selectUrl(houseSourse.getId());
            requesBrowsingRecordsVo.setMainImg(houseUrl1.getUrl());
            if (houseUrls.get(0) != null) {
                requesBrowsingRecordsVo.setId1(houseUrls.get(0).getId());
                requesBrowsingRecordsVo.setUrl1(houseUrls.get(0).getUrl());
            }
            list.add(requesBrowsingRecordsVo);
        }
        return list;
    }

    /*新增房源*/

    @Override
    public int insert(HouseSourse houseSourse) {
      /*  HouseSourse houseSourse1 = new HouseSourse();
        houseSourse1.getId();*/
        houseSourseDao.insertSelective(houseSourse);
        return houseSourse.getId();
    }
    @Override
    public int insertSelective(HouseSourse houseSourse) {
        houseSourseDao.insertSelective(houseSourse);
        return houseSourse.getId();
    }

    //修改租房状态
    @Override
    public int updateState(HouseSourse houseSourse) {
        return houseSourseDao.updateState(houseSourse);
    }


/*    @Override
    public HouseUrlSourseVo queryById(Integer id) {
        HouseSourse houseSourse = houseSourseDao.selectByPrimaryKey(id);
        HouseUrl houseUrl = houseUrlDao.selectByPrimaryKey(houseSourse.getId());
        HouseUrlSourseVo houseUrlSourseVo = new HouseUrlSourseVo();
        houseUrlSourseVo.setId(houseUrl.getId());
        houseUrlSourseVo.setAddress(houseSourse.getAddress());

        return houseUrlSourseVo;
    }*/

    /**
     * 连表查询
     * @param id
     * @return
     */


}
